<?php
/**
 * 重签名页面
 * @author Giles
 * @package webscoket
 *
 */

namespace Statistics\Modules;
//namespace \Statistics\Lib\AutoPack;
function signature()
{
    //check_auth();
    //$codesign =  AutoPack::run();
    //$codesign->run();
    //$userName = system('whoami');
    $userName = getUser();
    $packPath = '/Users/'.$userName.'/workspace/workCodes/socket/Applications/AutoPack';
    $downPath = '/Users/'.$userName.'/workspace/workCodes/upload/package';

    $package = glob($packPath.'/ipa/*.ipa');
    $packName = array();
    foreach($package as $key => $pack)
    {   
        $tmp = explode('/', $pack);
        $packName[$key] =  $tmp[9];
    }

    if($userName == 'Giles') 
    {
        $downIp = 'http://127.0.0.1';
        $downUrl = 'http://upload.giles.cc';
    } else {
        $downIp = 'http://192.168.1.157';
        $downUrl = 'http://192.168.1.157:8080';
    }

    $configAll = glob($packPath.'/mobileprovision/*.mobileprovision');
    $configName = array();
    foreach($configAll as $key => $config)
    {
        $tmp = explode('/', $config);
        $confName[$key] =  $tmp[9];
    }

    $jpush = array(
        "com.dongtang.helper"=> "3e9203b33c474288e292f2b5",
        "com.gaoke.helper"=> "f32486e7fc7a3644c596c2d8",
        "com.sf.helper"=> "bb4ef618a977e0bac7dd203b",
        "com.metal.helper"=> "17311c0c216f0231677d3fa5",
        "com.derui.helper"=> "b685e339fc27b1170a805403",
        "com.baiyun.helper"=> "7b69cc65969996cccad09ad2",
        "com.wugu.helper"=> "66ccbb9629a6a9aa041cd816",
        "com.suzhong.helper"=> "e23d74f00e102a9614c03179",
        "com.newland.helper"=> "1fa0be7417e11a7d37f582ee",
        "com.huihua.helper"=> "c03f6a7f6b24d6c36da7fd3c",
        "com.FeriaDigital.helper"=> "b781a63dec9aa6202dd3ffa1",
        "com.GuangzhouBomeng.helper"=> "3f783ed25a5bb004aaeffef0",
        "com.KUNMINGYUNNEIPOWER.helper"=> "088520f11a381647f0524df9",
        "com.GoldenBright.helper"=> "1866225df69e9079b2822e93",
        "com.Looyet.helper"=> "09dfc0972177df71621cb7fe",
        "com.PharmaLab.helper"=> "89ce53b76be94ae94f9f0306",
        "com.Catalyst.helper"=> "39ec81e08d34421d41022e13",
        "com.Eventplus.helper"=> "38489f7368708024a4faa9be",
        "com.Column.helper"=> "d5a25beeeadc933fc7a8865f",
        "com.DeKong.helper"=> "1b85e8074b3cfef6ed34a707",
        "com.Heavy.helper"=> "57453ecbd6321f3b050a6f5b",
        "com.Fable.helper"=> "ceb549c6568da6912d626bdf",
        "com.CardCity.helper"=> "f707465ec4a71aded6bfc525",
        "com.Coninst.helper"=> "fb2980f3e2a721a4bb6ef1c2",
        "com.Linvison.helper"=> "555ac2e92db3ee6c234801f7",
        "com.ShuangFei.helper"=> "01ff8cc308cad5cec9533cdc",
        "com.Longjie.helper"=> "71d3e9da235c6b4272f234b0",
        "com.Jiaming.helper"=> "5b3da862bb4a72a5822b138d",
        "com.Shengfuli.helper"=> "0d357a8a4b23f02d41e4e3c8",
        "com.Guobang.helper"=> "9c0515352c442d1128fee458",
        "com.Yincheng.helper"=> "44edce971b184ecb3ceb93b3",
        "com.Yuebo.helper"=> "0da5335024a0414c48b7471a",
        "com.Kerulen.helper"=> "18681f28529617eae51d548e",
        "com.Zhongde.helper"=> "46ce3b178fa8bf0592219179",
        "com.Panasign.helper"=> "458bce48d45b244cccc98a89",
        "com.AOKMAN.helper"=> "8811c11c2f3c7a4c1a4d4a8d",
        "com.Fangsheng.helper"=> "de58537ad1c3780146f6ddb3",
        "com.Jingxuan.helper"=> "d7a7ca7a10117c951920d3c0",
        "com.Qitongbao.helper"=> "0f17007c48263a3df1fff7e4",
        "com.Wanqu.helper"=> "39e038a8204c9e876cf32d26",
        "com.JieDaXing.helper"=> "b8c52bffcb03a97c83bd5d83",
        "com.Aromas.helper"=> "68bb0130b9340bf13f1dd8fc",
        "com.Yongbang.helper"=> "fa33f0abcfb977245e6f2b1a"
    );

    $channel = getAllChannel();
    $allGame = getAllGame();
    
    $types = isset($_GET['type']) ? $_GET['type'] : '';
    $packageName = isset($_GET['packName']) ? $_GET['packName'] : '';
    $configNames = isset($_GET['confName']) ? $_GET['confName'] : '';
    $channels = isset($_GET['channel']) ? $_GET['channel'] : '';
    $softId = isset($_GET['softId']) ? $_GET['softId'] : '';

    $url ="#";
    if(!empty($packageName) && !empty($configNames) && !empty($channels)) {
        $cmdXml = "security cms -D -i ". $packPath."/mobileprovision/".$configNames ." > ".$packPath."/temp.xml";
        system($cmdXml);
        $xmlData = simplexml_load_file($packPath."/temp.xml");
        $string = (array) $xmlData->dict->string;
        
        $return['certificate'] = 'iPhone Distribution: '.$string[2];
        
        $bundleTemp = (array) $xmlData->dict->dict->string;
        $bundlitem = $bundleTemp[0];
        $temp = explode('.', $bundlitem);
        $return['itermid'] = $temp[0];
        unset($temp[0]);
        $return['bundleId'] = implode('.', $temp);
        if(empty($return['bundleId'])) {
            $bundlitem = $bundleTemp[2];
            $temp = explode('.', $bundlitem);
            $return['itermid'] = $temp[0];
            unset($temp[0]);
            $return['bundleId'] = implode('.', $temp);
        }
        $return['certificatemd5'] = md5($return['certificate']);

        if($types == 'iiapple') {
            $return['jpushappkey'] = isset($jpush[$return['bundleId']]) ? $jpush[$return['bundleId']] : 'null';
            $shellPath="/Users/".$userName."/workspace/workCodes/socket/Applications/signpack.sh";
            $cmd = "sh ".$shellPath."  ".$packageName. " ".$configNames." '". $return['certificate']."' '".$return['itermid']."' '".$return['bundleId']."' '".$return['certificatemd5']."' ".$channels." ".$return['jpushappkey'];
        } else {
            $shellPath="/Users/".$userName."/workspace/workCodes/socket/Applications/signpack.sh";
            $cmd = "sh ".$shellPath."  ".$packageName. " ".$configNames." '". $return['certificate']."' '".$return['itermid']."' '".$return['bundleId']."' '".$return['certificatemd5']."' ".$channels; 
        }
        $res = system($cmd);

        $fileNameold = rtrim($packageName, '.ipa');
        //var_dump($fileNameold);
        $oldName = $downPath .'/'.$fileNameold.'.'. $channels.'.ipa';
        //var_dump($oldName);

        //MD5_softid_渠道标示_bundleid_渠道号来命名
        $filemd5 = md5_file($oldName);

        if($channels == '1001000101') {
            $namechannel = 1;
        } else if($channels == '2001000101') {
            $namechannel = 2;
        } else if($channels == '3001000101') {
            $namechannel = 3;
        } else {
            $namechannel = 4;
        }
        //var_dump($filemd5);
        $fileNames = $filemd5 .'_'. $softId. '_' .$namechannel .'_'.$fileNameold.'_'.$channels.'.ipa';
        //var_dump($fileNames);
        rename($oldName, $downPath.'/'.$fileNames);

        file_put_contents($downPath.'/filename', $fileNames);

    }


    include ST_ROOT . '/Views/header.tpl.php';
    include ST_ROOT . '/Views/signature.tpl.php';
    include ST_ROOT . '/Views/footer.tpl.php';

}